package com.situ.b2c.product.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.SimpleFormatter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

import com.situ.b2c.catalog.service.CatalogService;
import com.situ.b2c.pojo.Product;
import com.situ.b2c.product.service.ProductService;
import com.situ.b2c.product.service.impl.ProductServiceImpl;

@WebServlet("/ProductAddServlet")
public class ProductAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProductService productService;
	private Product product;

	public ProductAddServlet() {
		ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
		productService = context.getBean(ProductService.class);
		product = context.getBean(Product.class);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		

		String cataId = request.getParameter("cataId");
		String prodName = request.getParameter("prodName");
		String marketPrice = request.getParameter("marketPrice");
		String vipPrice = request.getParameter("vipPrice");
		String prodQuantity = request.getParameter("prodQuantity");
		String isSale = request.getParameter("isSale");
		String prodDescription = request.getParameter("prodDescription");
		String createDate = request.getParameter("createDate");
		String createBy = request.getParameter("createBy");
		String activeFlag = request.getParameter("activeFlag");

		product.setCataId(Long.parseLong(cataId));
		product.setProdName(prodName);
		product.setMarketPrice(Double.parseDouble(marketPrice));
		product.setVipPrice(Double.parseDouble(vipPrice));
		product.setProdQuantity(Integer.parseInt(prodQuantity));
		product.setIsSale(Integer.parseInt(isSale));
		product.setProdDescription(prodDescription);
		try {
			product.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").parse(createDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		product.setCreateBy(createBy);
		product.setActiveFlag(Integer.parseInt(activeFlag));

		productService.insert(product);
		response.sendRedirect("ProductSelectAllServlet");

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
